package com.wtwd.campus.fjryE15.untils;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.utils.DateUtils;
import com.wtwd.campus.utils.HttpclientUtil;
import com.wtwd.campus.utils.MD5Utils;
import com.wtwd.campus.wx.utils.AESUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Random;

/**
 * @author mjy
 * 此处用于校园云对接(福建睿翼e15)
 * @date 2022/08/17
 * @description
 */
public class XyyUtiils {

    private static final Logger logger = LoggerFactory.getLogger(XyyUtiils.class);

    public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String APP_ID = "113";

    public static String APP_KEY = "6JPpE4kmceU1d0fN";

    //以下为请求路径
    //查询用户信息
    private static String USER_INFO_URL = "https://yzs.fj.cn/portal/openapi/user/getUserInfo";

    //围栏进出推送
    private static String FENCE_IN_AND_OUT = "https://yzs.fj.cn/portal/openapi/msgPush/fenceReport";

    //sos上报
    private static String SOS_REPORT_URL = "https://yzs.fj.cn/portal/openapi/msgPush/sosReport";

    //位置上报
    private static String POSITION_REPORT_URL = "https://yzs.fj.cn/portal/openapi/msgPush/positionReport";

    //低电量告警
    private static String BATTERY_REPORT_URL = "https://yzs.fj.cn/portal/openapi/msgPush/batteryReport";

    /**
     * 根据睿翼e15token获取用户信息
     *
     * @pa token
     */
    public static RyE15UserInfoData getRyE15UserInfo(String token) {
        String randomStr = generateStr(8);
        String time = DateUtils.format(new Date(), DateUtils.dateFormat15);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", APP_ID);
        jsonObject.put("time", time);
        jsonObject.put("randomStr", randomStr);
        jsonObject.put("sign", getRySign(APP_ID, time, randomStr, APP_KEY));
        jsonObject.put("token", token);
        logger.info("send http ryE15 getRyE15UserInfo param={}", jsonObject.toJSONString());
        String s = HttpclientUtil.httpPost(USER_INFO_URL, jsonObject);
        RyE15UserInfoData ryE15UserInfoData = null;
        logger.info("receive http ryE15 getRyE15UserInfo param={}", s);
        try {
            ryE15UserInfoData = JSONObject.parseObject(s, RyE15UserInfoData.class);
            ryE15UserInfoData.setRyE15StudentInfo(JSONObject.parseObject(AESUtils.deCode(ryE15UserInfoData.getData().getStudentInfo(), APP_KEY), RyE15StudentInfo.class));
            ryE15UserInfoData.setRyE15ParentInfo(JSONObject.parseObject(AESUtils.deCode(ryE15UserInfoData.getData().getStudentInfo(), APP_KEY), RyE15ParentInfo.class));
        } catch (Exception e) {
            logger.info("receive http ryE15 getRyE15UserInfo param={}", s);
            e.printStackTrace();
        } finally {
            return ryE15UserInfoData;
        }
    }

    public static void main(String[] args) {
       /* RyE15FenceReport ryE15FenceReport = new RyE15FenceReport();
        ryE15FenceReport.setImei("86869405466620");
        ryE15FenceReport.setFenceInfo(JSONObject.toJSONString(new FenceInfo()));
        ryE15FenceReport.setLng(String.valueOf(113.947329));
        ryE15FenceReport.setLat(String.valueOf(22.565894));
        ryE15FenceReport.setPosition("cs");
        String rylocationType = "";
        ryE15FenceReport.setMode("1");
        ryE15FenceReport.setType(String.valueOf("1"));
        ryE15FenceReport.setReportTime(DateUtils.format(new Date(), DateUtils.dateFormat15));
        XyyUtiils.pushFenceInfo(ryE15FenceReport);
        logger.info("******************************************************************************");
        SosReport sosReport = new SosReport();
        sosReport.setImei("86869405466620");
        sosReport.setLng(String.valueOf(113.947329));
        sosReport.setLat(String.valueOf(22.565894));
        sosReport.setPosition("cs");
        sosReport.setReportTime(DateUtils.format(new Date(), DateUtils.dateFormat15));
        XyyUtiils.pushSosReport(sosReport);
        logger.info("******************************************************************************");
        PositionReport positionReport = new PositionReport();
        positionReport.setImei("86869405466620");
        positionReport.setLat(String.valueOf(22.565894));
        positionReport.setLng(String.valueOf(113.947329));
        positionReport.setPosition("cs");
        positionReport.setPushType("0");
        Date date = new Date();
        positionReport.setReportTime(DateUtils.format(date, DateUtils.dateFormat15));
        XyyUtiils.pushPositionReport(positionReport);
        logger.info("******************************************************************************");
        BatteryReport batteryReport = new BatteryReport();
        batteryReport.setImei("86869405466620");
        batteryReport.setBattery("15");
        batteryReport.setReportTime(DateUtils.format(new Date(),DateUtils.dateFormat15));
        XyyUtiils.pushBatteryReport(batteryReport);
        logger.info("******************************************************************************");
*/
   /*     String s = AESUtils.enCode("[{\"userName\":\"cs\",\"phone\":\"13669061320\"}]", APP_KEY);
        String s1 = AESUtils.deCode("xmnP6AfZbuhccga5L8z20A==", APP_KEY);*/
   /*
        System.out.println(s);
        System.out.println(s1);*/

        /*String imei = AESUtils.enCode("[{\n" +
                "     \t\"userName\":\"cs\",\n" +
                "     \t\"phone\":\"13669061320\"\n" +
                "     },\n" +
                "    {\n" +
                "     \t\"userName\":\"cs1\",\n" +
                "     \t\"phone\":\"13669061321\"\n" +
                "     },\n" +
                "     {\n" +
                "     \t\"userName\":\"cs2\",\n" +
                "     \t\"phone\":\"13669061322\"\n" +
                "     }\n" +
                "     ]", APP_KEY);
        System.out.println(imei);*/
      /*  String rySign = getRySign(APP_ID, "445436", "123456", APP_KEY);
        System.out.println(rySign);*/
        String s = AESUtils.enCode("863561050082855", APP_KEY);
        System.out.println(s);
    }

    /**
     * 推送围栏信息
     *
     * @param ryE15FenceReport
     */
    public static void pushFenceInfo(RyE15FenceReport ryE15FenceReport) {
        String randomStr = generateStr(8);
        String time = DateUtils.format(new Date(), DateUtils.dateFormat15);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", APP_ID);
        jsonObject.put("time", time);
        jsonObject.put("randomStr", randomStr);
        jsonObject.put("sign", getRySign(APP_ID, time, randomStr, APP_KEY));
        jsonObject.put("imei", AESUtils.enCode(ryE15FenceReport.getImei(),APP_KEY));
        jsonObject.put("lng", ryE15FenceReport.getLng());
        jsonObject.put("lat", ryE15FenceReport.getLat());
        jsonObject.put("position", ryE15FenceReport.getPosition());
        jsonObject.put("mode", ryE15FenceReport.getMode());
        jsonObject.put("fenceInfo", "");
        jsonObject.put("type", ryE15FenceReport.getType());
        jsonObject.put("reportTime", ryE15FenceReport.getReportTime());
        logger.info("send http ryE15 pushFenceInfo param={}", jsonObject.toJSONString());
        String s = HttpclientUtil.httpPost(FENCE_IN_AND_OUT, jsonObject);
        logger.info("receive http ryE15 pushFenceInfo param={}", s);
    }

    /**
     * 推送sos告警
     *
     * @param sosReport
     */
    public static void pushSosReport(SosReport sosReport) {
        String randomStr = generateStr(8);
        String time = DateUtils.format(new Date(), DateUtils.dateFormat15);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", APP_ID);
        jsonObject.put("time", time);
        jsonObject.put("randomStr", randomStr);
        jsonObject.put("sign", getRySign(APP_ID, time, randomStr, APP_KEY));
        jsonObject.put("imei", AESUtils.enCode(sosReport.getImei(),APP_KEY));
        jsonObject.put("lng", sosReport.getLng());
        jsonObject.put("lat", sosReport.getLat());
        jsonObject.put("position", sosReport.getPosition());
        jsonObject.put("reportTime", sosReport.getReportTime());
        logger.info("send http ryE15 pushSosReport param={}", jsonObject.toJSONString());
        String s = HttpclientUtil.httpPost(SOS_REPORT_URL, jsonObject);
        logger.info("receive http ryE15 pushSosReport param={}", s);
    }

    /**
     * 推送定位数据
     *
     * @param positionReport
     */
    public static void pushPositionReport(PositionReport positionReport) {
        String randomStr = generateStr(8);
        String time = DateUtils.format(new Date(), DateUtils.dateFormat15);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", APP_ID);
        jsonObject.put("time", time);
        jsonObject.put("randomStr", randomStr);
        jsonObject.put("sign", getRySign(APP_ID, time, randomStr, APP_KEY));
        jsonObject.put("imei", AESUtils.enCode(positionReport.getImei(),APP_KEY));
        jsonObject.put("lng", positionReport.getLng());
        jsonObject.put("lat", positionReport.getLat());
        jsonObject.put("position", positionReport.getPosition());
        jsonObject.put("pushType", positionReport.getPushType());
        jsonObject.put("reportTime", positionReport.getReportTime());
        logger.info("send http ryE15 pushPositionReport param={}", jsonObject.toJSONString());
        String s = HttpclientUtil.httpPost(POSITION_REPORT_URL, jsonObject);
        logger.info("receive http ryE15 pushPositionReport param={}", s);
    }

    /**
     * 推送低电量告警
     */
    public static void pushBatteryReport(BatteryReport batteryReport) {
        String randomStr = generateStr(8);
        String time = DateUtils.format(new Date(), DateUtils.dateFormat15);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId", APP_ID);
        jsonObject.put("time", time);
        jsonObject.put("randomStr", randomStr);
        jsonObject.put("sign", getRySign(APP_ID, time, randomStr, APP_KEY));
        jsonObject.put("imei", AESUtils.enCode(batteryReport.getImei(),APP_KEY));
        jsonObject.put("battery", batteryReport.getBattery());
        jsonObject.put("reportTime", batteryReport.getReportTime());
        logger.info("send http ryE15 pushBatteryReport param={}", jsonObject.toJSONString());
        String s = HttpclientUtil.httpPost(BATTERY_REPORT_URL, jsonObject);
        logger.info("receive http ryE15 pushBatteryReport param={}", s);
    }


    /**
     * 获取签名
     *
     * @param appId
     * @param time
     * @param randomStr
     * @param appKey
     * @return
     */
    public static String getRySign(String appId, String time, String randomStr, String appKey) {
        String str = "appId=" + appId + "&randomStr=" + randomStr + "&time=" + time + "&key=" + appKey;
        return MD5Utils.encode(str).toUpperCase();
    }

    /**
     * 获取随机字符串
     *
     * @param len
     * @return
     */
    public static String generateStr(int len) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        return sb.toString();
    }


}
